﻿using System;
using System.Collections.Generic;
using System.Text;
using DataAccess;
using Entities;

namespace Logic
{
	public class TagBL
	{
		public bool Save(Tag entity)
		{
			TagDAL dal = new TagDAL();

			if (dal.GetByName(entity.Name) == null)
				return dal.Save(entity);
			else
				return true;
		}

        public bool Update(Tag entity)
        {
            TagDAL dal = new TagDAL();
            return dal.Update(entity);
        }

        public bool Delete(Tag entity)
        {
            TagDAL dal = new TagDAL();
            return dal.Delete(entity);
        }

		public bool AddMovie(Tag entity, Movie movie)
		{
			TagDAL dal = new TagDAL();
			return dal.AddMovie(entity, movie);
		}

		public bool DeleteMovie(Tag entity, Movie movie)
		{
			TagDAL dal = new TagDAL();
			return dal.DeleteMovie(entity, movie);
		}

		public Tag GetByName(string name)
		{
			TagDAL dal = new TagDAL();
			return dal.GetByName(name);
		}

		public Tag GetLastAdded()
		{
			TagDAL dal = new TagDAL();
			return dal.GetLastAdded();
		}

		public List<Tag> List()
		{
			TagDAL dal = new TagDAL();
			return dal.List();
		}

		public List<Movie> GetMovies(Tag entity)
		{
			MovieDAL mdal = new MovieDAL();
			return mdal.List("SELECT movies.* FROM movies LEFT JOIN relationships ON movies.movie_id=relationships.movie_id WHERE relationships.relationship_element=" + entity.Id + " AND relationships.relationship_type='tag'");
		}
	}
}
